package com.coe3.data;

import java.util.HashMap;

import android.content.Context;
import android.util.Log;

public class SessionManager {
	
	// All Shared Preferences Keys
	private static final String IS_LOGIN = "IsLoggedIn";
	
	// Email address (make variable public to access from outside)
	public static final String KEY_EMAIL = "email";
	
	// Email address (make variable public to access from outside)
	public static final String KEY_PASSWORD = "password";
	
	/**
	 * Create login session
	 * */
	public static void createLoginSession(Context context, String email, String pass){
		// Storing login value as TRUE
		DataStorage.setBoolData(context, IS_LOGIN, true);
		
		// Storing email in pref
		DataStorage.setStrData(context, KEY_EMAIL, email);
		
		// Storing name in pref
		DataStorage.setStrData(context, KEY_PASSWORD, pass);
	}	
	
	/**
	 * 
	 * @param context
	 * @param email
	 * @param pass
	 * @return true: is have user.
	 * @return false: is don't have user or dont't found email session.
	 */
	public static Boolean checkUser(Context context, String email, String pass) {
		HashMap<String, String> user = getUserDetails(context);
		String mEmail = user.get(KEY_EMAIL);
		
		Log.d(Information.TAG_DBUG, "mEmail: " + mEmail);
		Log.d(Information.TAG_DBUG, "email: " + email);
		
		if(mEmail != null && mEmail.equals(email)){
			return true;
		} else {
			return false;
		}//end if
	}
	
	
	/**
	 * Get stored session data
	 * */
	public static HashMap<String, String> getUserDetails(Context context){
		HashMap<String, String> user = new HashMap<String, String>();

		// user email id
		user.put(KEY_EMAIL, DataStorage.getStrData(context, KEY_EMAIL, null));
		
		// user name
		user.put(KEY_PASSWORD, DataStorage.getStrData(context, KEY_PASSWORD, null));
		
		// return user
		return user;
	}
	
	/**
	 * Login.
	 */
	public static Boolean loginUser(Context context){
		return DataStorage.setBoolData(context, IS_LOGIN, true);
	}
	
	/**
	 * Clear session details
	 * */
	public static void logoutUser(Context context){
		// Clearing all data from Shared Preferences
		DataStorage.setBoolData(context, IS_LOGIN, false);
	}
	
	/**
	 * Quick check for login
	 * **/
	// Get Login State
	public static boolean isLoggedIn(Context context){
		return DataStorage.getBoolData(context, IS_LOGIN, false);
	}
}
